package com.openxt.nbzj.plugins;

import android.content.Context;
import android.util.Log;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

/**
 * Created by 颖杰_2 on 2017-07-02.
 */

public class AppPlugins {
    public static class Plugin {
        private String name;
        private String version;
        private String url;
        private String packageName;

        public Plugin() {

        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getPackageName() {
            return packageName;
        }

        public void setPackageName(String packageName) {
            this.packageName = packageName;
        }
    }

    private static List<Plugin> list = new ArrayList<>();

    public static void init(Context context) {
        list.clear();
        RequestParams requestParams = new RequestParams("https://zjshenyue.cn/app/yczj/android/plugins.xml");
        x.http().get(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder _builder = factory.newDocumentBuilder();
                    Document document = _builder.parse(new ByteArrayInputStream(result.getBytes()));
                    Element root = document.getDocumentElement();
                    NodeList pluginNodes = root.getElementsByTagName("plugin");
                    for (int i = 0, j = pluginNodes.getLength(); i < j; i++) {
                        Element pluginEl = (Element) pluginNodes.item(i);
                        Plugin plugin = new Plugin();
                        NodeList childNodes = pluginEl.getChildNodes();
                        for (int y = 0, x = childNodes.getLength(); y < x; y++) {
                            if (childNodes.item(y).getNodeType() == Node.ELEMENT_NODE) {
                                Element el = (Element) childNodes.item(y);
                                if ("name".equals(el.getNodeName())) {
                                    plugin.setName(el.getTextContent());
                                } else if ("version".equals(el.getNodeName())) {
                                    plugin.setVersion(el.getTextContent());
                                } else if ("url".equals(el.getNodeName())) {
                                    plugin.setUrl(el.getTextContent());
                                } else if ("package".equals(el.getNodeName())) {
                                    plugin.setPackageName(el.getTextContent());
                                }
                            }
                        }
                        list.add(plugin);
                    }
                } catch (Exception ex) {
                    Log.e("TAG", ex.getMessage(), ex);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    public static Plugin getPlugin(String packageName) {
        for (int i = 0, j = list.size(); i < j; i++) {
            Plugin plugin = list.get(i);
            if (plugin.getPackageName().equals(packageName))
                return plugin;
        }

        return null;
    }
}
